package com.simon.batteryinformation;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class BatteryDBAdapter {

	public static final String KEY_ROWID = "_id";
	public static final String KEY_TIME = "time";
	public static final String KEY_LEVEL = "level";
	public static final String KEY_HEALTH = "health";
	public static final String KEY_HEALTH_RAW = "healthraw";
	public static final String KEY_TECHNOLOGY = "technology";
	public static final String KEY_PLUGGED = "plugged";
	public static final String KEY_PLUGGED_RAW = "pluggedraw";
	public static final String KEY_STATUS = "status";
	public static final String KEY_STATUS_RAW = "statusraw";
	public static final String KEY_VOLT = "volt";
	public static final String KEY_VOLT_RAW = "voltraw";
	public static final String KEY_TEMP = "temp";
	public static final String KEY_TEMP_RAW = "tempraw";

	private static final String TAG = "GPSDbAdapter";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	/**
	 * Database creation sql statement
	 */
	private static final String DATABASE_CREATE = "create table batterydata (_id integer primary "
			+ "key autoincrement, "
			+ "time real, level integer, health text, healthraw integer, "
			+ "technology text, plugged text, pluggedraw integer,"
			+ "status text, statusraw integer, volt text, voltraw integer, temp text, tempraw integer);";

	private static final String DATABASE_NAME = "batteryinfo";
	private static final String DATABASE_TABLE = "batterydata";
	private static final int DATABASE_VERSION = 2;

	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS notes");
			onCreate(db);
		}
	}

	/**
	 * Constructor - takes the context to allow the database to be
	 * opened/created
	 * 
	 * @param ctx
	 *            the Context within which to work
	 */
	public BatteryDBAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	/**
	 * Open the database. If it cannot be opened, try to create a new instance
	 * of the database. If it cannot be created, throw an exception to signal
	 * the failure
	 * 
	 * @return this (self reference, allowing this to be chained in an
	 *         initialization call)
	 * @throws SQLException
	 *             if the database could be neither opened or created
	 */
	public BatteryDBAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	/**
	 * Create a new data. If the data is successfully created return the new
	 * rowId for inserted data, otherwise return a -1 to indicate failure.
	 * 
	 * @param time
	 *            the time of data insertion
	 * @param level
	 *            the level of battery charge
	 * @param helath
	 *            the health of battery
	 * @param healthraw
	 *            the raw information about battery health
	 * @param technology
	 *            the technology of battery
	 * @param plugged
	 *            information about that battery is plugged to external source
	 *            of power
	 * @param pluggedraw
	 *            raw information about that battery is plugged to external
	 *            source of power
	 * @param status
	 *            the status of battery
	 * @param statusraw
	 *            raw information about battery status
	 * @param volt
	 *            the volt of battery
	 * @param voltraw
	 *            raw information about battery volt
	 * @param temp
	 *            the temperature of battery
	 * @param tempraw
	 *            raw information about battery temperature
	 * @return rowId or -1 if failed
	 */

	public long create(Long time, Integer level, String health,
			Integer healthraw, String technology, String plugged,
			Integer pluggedraw, String status, Integer statusraw, String volt,
			Integer voltraw, String temp, Integer tempraw) {

		ContentValues initialValues = new ContentValues();

		initialValues.put(KEY_TIME, time);
		initialValues.put(KEY_LEVEL, level);
		initialValues.put(KEY_HEALTH, health);
		initialValues.put(KEY_HEALTH_RAW, healthraw);
		initialValues.put(KEY_TECHNOLOGY, technology);
		initialValues.put(KEY_PLUGGED, plugged);
		initialValues.put(KEY_PLUGGED_RAW, pluggedraw);
		initialValues.put(KEY_STATUS, status);
		initialValues.put(KEY_STATUS_RAW, statusraw);
		initialValues.put(KEY_VOLT, volt);
		initialValues.put(KEY_VOLT_RAW, voltraw);
		initialValues.put(KEY_TEMP, temp);
		initialValues.put(KEY_TEMP_RAW, tempraw);

		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	/**
	 * Delete the row with the given rowId
	 * 
	 * @param rowId
	 *            id of note to delete
	 * @return true if deleted, false otherwise
	 */
	public boolean deleteData(long rowId) {

		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Return a Cursor over the list of all datas in the database
	 * 
	 * @return Cursor over all battery data.
	 */

	public Cursor fetchAllGpsInfos() {
		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TIME,
				KEY_LEVEL, KEY_HEALTH, KEY_HEALTH_RAW, KEY_TECHNOLOGY,
				KEY_PLUGGED, KEY_PLUGGED_RAW, KEY_STATUS, KEY_STATUS_RAW,
				KEY_VOLT, KEY_VOLT_RAW, KEY_TEMP, KEY_TEMP_RAW }, null, null,
				null, null, null);
	}

	/**
	 * Return a Cursor positioned at the data that matches the given rowId
	 * 
	 * @param rowId
	 *            id of note to retrieve
	 * @return Cursor positioned to matching data, if found
	 * @throws SQLException
	 *             if note could not be found/retrieved
	 */
	public Cursor fetchGpsInfo(long rowId) throws SQLException {

		Cursor mCursor = mDb.query(true, DATABASE_TABLE,
				new String[] { KEY_ROWID, KEY_TIME, KEY_LEVEL, KEY_HEALTH,
						KEY_HEALTH_RAW, KEY_TECHNOLOGY, KEY_PLUGGED,
						KEY_PLUGGED_RAW, KEY_STATUS, KEY_STATUS_RAW, KEY_VOLT,
						KEY_VOLT_RAW, KEY_TEMP, KEY_TEMP_RAW }, KEY_ROWID + "="
						+ rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

}